package cn.com.pureAppApi.interceptor;

import java.net.URLEncoder;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.method.HandlerMethod;

/**
 * QQ讨论群:76989293
 * @email 2696701142@qq.com
 * @date 2018-06-12 16:51:48
 *
 */
public class WebHandlerUtil {
	
	final static Log logger = LogFactory.getLog(WebHandlerUtil.class);

	/**
	 * 在方法执行之前拦截
	 * 
	 * @param request
	 * @param response
	 * @param handler
	 * @param openid 
	 * @param hearder 
	 * @return
	 * @throws Exception
	 */
	public static cn.com.pureAppApi.interceptor.RequestHeader preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		cn.com.pureAppApi.interceptor.RequestHeader header = null;
		//获取默认请求值
		String token = request.getHeader(HeaderEnum.TOKEN.getKey());
		if(request.getCookies()!=null){
			for(Cookie cookie : request.getCookies()){
				if(cookie.getName().equals("token")){
					token=cookie.getValue();
				}
			}
		}
		String messageID = request.getHeader(HeaderEnum.MESSAGE_ID.getKey());
		Date date = null;
		if(org.springframework.util.StringUtils.isEmpty(request.getHeader(HeaderEnum.DATE.getKey()))){
			date = new Date();
		}else{
			date = new Date(Long.parseLong(request.getHeader(HeaderEnum.DATE.getKey())));
		}
		request.setAttribute("date", date);
		Integer terminal = request.getIntHeader(HeaderEnum.TERMINAL.getKey());
		String version = request.getHeader(HeaderEnum.VERSION.getKey());
		String imei = request.getHeader(HeaderEnum.IMEI.getKey());
		String ua = request.getHeader(HeaderEnum.UA.getKey());
		String sysVersion = request.getHeader(HeaderEnum.SYSVERSION.getKey());
		Integer networkOper = request.getIntHeader(HeaderEnum.NETWORK_OPER.getKey());
		Integer networkStatus = request.getIntHeader(HeaderEnum.NETWORK_STATUS.getKey());
		String outsourceSign = request.getHeader(HeaderEnum.OUTSOURCE_SIGN.getKey());
		header = new cn.com.pureAppApi.interceptor.RequestHeader(token, messageID, date, terminal, version, imei, ua, networkOper, networkStatus, sysVersion, outsourceSign);
		//封装请求对象
		if(handler instanceof HandlerMethod){
			HandlerMethod handlerMethod = (HandlerMethod)handler;
			for(MethodParameter parameter:handlerMethod.getMethodParameters()){
				if(parameter.getParameterType().equals(RequestHeader.class)){
					//jdk1.8需要手动开启查询方法参数名，古此处做多种处理
					String name = parameter.getParameterName();
					if(name==null){
						name = parameter.getParameterAnnotation(RequestAttribute.class).name();
						if(name==null){
							name = parameter.getParameterAnnotation(RequestAttribute.class).value();
						}
					}
					request.setAttribute(StringUtils.isEmpty(name)?"header":name, header);
				}
			}
		}
		//设置默认响应
		response.setHeader(HeaderEnum.MESSAGE_ID.getKey(), request.getHeader(HeaderEnum.MESSAGE_ID.getKey()));
		response.setIntHeader(HeaderEnum.CODE.getKey(), 0);
		response.setHeader(HeaderEnum.MESSAGE.getKey(), URLEncoder.encode("", "UTF-8"));
		response.setHeader("Access-Control-Expose-Headers", "Code,Message,Message-ID,Date");
		return header;
	}

}
